package com.seaside.park.service;

import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seaside.common.bean.Result;
import com.seaside.parkCommon.bean.ParkPlace;
import com.seaside.parkCommon.bean.ParkZone;
import com.seaside.parkCommon.mapper.ParkPlaceMapper;
import com.seaside.parkCommon.mapper.ParkZoneMapper;

/**
 *
 * @author <a href="mailto:hoocoral@hotmail.com">Kunrong.HOo</a>
 * @version 1.0
 */
@Service
public class ParkService  {

	@Autowired
	private ParkZoneMapper parkZoneMapper;

	@Autowired
	private ParkPlaceMapper parkPlaceMapper;

	public Result<ParkZone> getParkZoneByNo(String parkZoneNo) {

		if (StringUtils.isBlank(parkZoneNo)) {
			return Result.Error("参数错误！");
		}

		QueryWrapper<ParkZone> wrapper = new QueryWrapper<>();
		wrapper.eq("zone_no", parkZoneNo);
		ParkZone parkZone = parkZoneMapper.selectOne(wrapper);

		if (parkZone != null) {
			return Result.OK("查询成功！", parkZone);
		}
		return Result.Error("查询失败！");
	}

	/**
	 * 查询所有停车场列表
	 * @return
	 */
	public Result<List<ParkZone>> getAllParkZoneWithPlaceList() {
		QueryWrapper<ParkZone> wrapper = new QueryWrapper<>();
		wrapper.orderByAsc("zone_id");
		List<ParkZone> parkZoneList = parkZoneMapper.selectList(wrapper);
		if (!CollectionUtils.isEmpty(parkZoneList)) {
			parkZoneList.forEach(zone -> {
				if (zone != null) {
					QueryWrapper<ParkPlace> placeWrapper = new QueryWrapper<>();
					placeWrapper.eq("zone_id", zone.getZoneId());
					placeWrapper.orderByAsc("park_no");

					List<ParkPlace> parkPlaceList = parkPlaceMapper.selectList(placeWrapper);
					if (!CollectionUtils.isEmpty(parkPlaceList)) {
						List<String> parkNoList = parkPlaceList.stream().map(ParkPlace::getParkNo).collect(Collectors.toList());
						zone.setParkNoList(parkNoList);
					}
				}
			});
		}
		return Result.OK("查询成功！", parkZoneList);
	}

	/**
	 * 查询所有停车场Map
	 * @return
	 */
	public Map<String, ParkZone> getAllParkZonesMap() {
		QueryWrapper<ParkZone> wrapper = new QueryWrapper<>();
		wrapper.orderByAsc("zone_id");
		List<ParkZone> parkZoneList = parkZoneMapper.selectList(wrapper);
		if (!CollectionUtils.isEmpty(parkZoneList)) {
			return parkZoneList.stream().collect(Collectors.toMap(ParkZone::getZoneNo, ParkZone -> ParkZone));
		}
		return new HashMap<String, ParkZone>();
	}

	public Result<List<ParkPlace>> getParkPlaceStatus(String parkZoneNo, String parkPlaceNos) {

		if (StringUtils.isBlank(parkZoneNo)) {
			return Result.Error("停车场编号为空！");
		}

		if (StringUtils.isBlank(parkPlaceNos)) {
			return Result.Error("泊位号为空！");
		}
		List<String> berthNumberList = Arrays.asList(parkPlaceNos.split(","));
		if (CollectionUtils.isEmpty(berthNumberList)) {
			return Result.Error("泊位号为空！");
		}

		Result<ParkZone> parkZoneRet = this.getParkZoneByNo(parkZoneNo);
		if (!parkZoneRet.isSucess()) {
			return Result.Error(parkZoneRet.getMsg());
		}

		QueryWrapper<ParkPlace> parkPlaceWrapper = new QueryWrapper<>();
		parkPlaceWrapper.eq("zone_id", parkZoneRet.getData().getZoneId());
		parkPlaceWrapper.in("park_no", berthNumberList);
		parkPlaceWrapper.orderByAsc("park_no");

		List<ParkPlace> parkPlaceList = parkPlaceMapper.selectList(parkPlaceWrapper);

//		List<ParkPlace> parkPlaceList = parkPlaceMapper.getParkPlaceByZoneNo(parkZoneNo, berthNumberList);
		if (CollectionUtils.isEmpty(parkPlaceList)) {
			return Result.Error("未找到指定泊位！");
		}
		return Result.OK("查询成功！", parkPlaceList);
	}

	/**
	 * 根据队列分配车位
	 */
	public ParkPlace OrderParkPlace(Queue<String> parkPlaceListQuery,int zoneId,int rounds){
		String parkPlacePreOrderNo;
		ParkPlace parkPlace;
		if(rounds > parkPlaceListQuery.size()){
			//遍历了所有车位，已经没有空闲车位,返回临停车位
			parkPlace = new ParkPlace();
			parkPlace.setParkType(0);
			parkPlace.setParkId(0);
			parkPlace.setParkNo("临停车位");
			return  parkPlace;
		}else{
			//顺序分配车位，从顺位队列中取出一个车位
			parkPlacePreOrderNo = parkPlaceListQuery.poll();
			//查看车位是否空闲
			QueryWrapper<ParkPlace> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("zone_id",zoneId).eq("park_no",parkPlacePreOrderNo);
			parkPlace = parkPlaceMapper.selectList(queryWrapper).get(0);
			if(StringUtils.isBlank(parkPlace.getCurrPlate())){
				parkPlaceListQuery.offer(parkPlacePreOrderNo);
				return parkPlace;
			}else{
				//有车的情况，到队列下一个
				parkPlaceListQuery.offer(parkPlacePreOrderNo);
				rounds += 1;
				return OrderParkPlace(parkPlaceListQuery,zoneId,rounds);
			}
		}
	}
}

